package zhihuilan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import zhihuilan.dto.MyAcquisitionMsgDto;
import zhihuilan.dto.WxMiniPageDto;
import zhihuilan.entity.MyAcquisitionMsg;
import zhihuilan.entity.MyAcquisitionMsgExtra;
import zhihuilan.mapper.MyAcquisitionMsgMapper;
import zhihuilan.params.query.MyAcquisitionMsgQueryParam;
import zhihuilan.service.MyAcquisitionMsgService;

import java.util.List;

/**
 * 采购数据 服务实现
 *
 * @author ruoyi
 */
@Service
public class MyAcquisitionMsgServiceImpl extends ServiceImpl<MyAcquisitionMsgMapper, MyAcquisitionMsg> implements MyAcquisitionMsgService {


    @Override
    public Page<MyAcquisitionMsg> selectPageList(MyAcquisitionMsgQueryParam queryParam) {
        QueryWrapper<MyAcquisitionMsg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        if (!ObjectUtils.isEmpty(queryParam)) {
            if (!ObjectUtils.isEmpty(queryParam.getPid())) {
                queryWrapper.eq("pid", queryParam.getPid());
            }
            if (!ObjectUtils.isEmpty(queryParam.getStaffId())) {
                queryWrapper.eq("staff_id", queryParam.getStaffId());
            }
        }
        queryWrapper.orderByDesc("create_time");
        long pageNum = 0;
        long pageSize = 10;
        if (queryParam.getPage() >= 0L) {
            pageNum = queryParam.getPage();
        }
        if (queryParam.getPageSize() > 0L) {
            pageSize = queryParam.getPageSize();
        }
        Page<MyAcquisitionMsg> page = new Page<>(pageNum, pageSize);
        this.baseMapper.selectPage(page, queryWrapper);
        return page;
    }

    @Override
    public WxMiniPageDto<MyAcquisitionMsgDto> selectMyAcquisitionMsgPage(MyAcquisitionMsgQueryParam queryParam) {
        long pageNum = 0;
        long pageSize = 10;
        if (queryParam.getPage() >= 0L) {
            pageNum = queryParam.getPage();
        }
        if (queryParam.getPageSize() > 0L) {
            pageSize = queryParam.getPageSize();
        }
        if(pageNum>0){
            pageNum=pageSize*(pageNum-1);
        }
        WxMiniPageDto<MyAcquisitionMsgDto> page = new WxMiniPageDto<>(pageNum, pageSize);
        List<MyAcquisitionMsgDto> myAcquisitionMsgExtra = this.baseMapper.selectMyAcquisitionMsgPage(pageNum, pageSize, queryParam.getPid(), queryParam.getStaffId(),queryParam.getStatus());
        Integer count = this.baseMapper.selectMyAcquisitionMsgCount(queryParam.getPid(), queryParam.getStaffId(),queryParam.getStatus());
        page.setData(myAcquisitionMsgExtra);
        page.setTotal(count);
        return page;
    }

    @Override
    public MyAcquisitionMsg selectById(Integer id) {
        MyAcquisitionMsg MyAcquisitionMsg = this.baseMapper.selectById(id);
        return MyAcquisitionMsg;
    }

    @Override
    public MyAcquisitionMsg addEntity(MyAcquisitionMsg entity) {
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public MyAcquisitionMsg updateEntity(MyAcquisitionMsg entity) {
        this.baseMapper.updateById(entity);
        return entity;
    }


    @Override
    public Boolean deleteById(Integer id) {
        if(ObjectUtils.isEmpty(id)){
            return false;
        }
        return this.baseMapper.deleteById(id)>0;
    }
}
